package com.ideal.manage.esms.service.alarm;
import com.ideal.manage.esms.bean.alarm.Alarm;
import com.ideal.manage.esms.bean.alarm.AlarmHistory;
import com.ideal.manage.esms.repository.alarm.AlarmHistoryRepository;
import com.ideal.manage.esms.repository.alarm.AlarmRepository;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AlarmHistoryService {
    @Autowired
    private AlarmRepository alarmRepository;

    @Autowired
    private AlarmHistoryRepository alarmHistoryRepository;


    //查找历史记录的id
    public AlarmHistory findAllAlarmDataById(String T){
        String id = "";
        List<AlarmHistory> allData = alarmHistoryRepository.findAll();
        for (AlarmHistory alarmHistory:allData) {
            if (alarmHistory.getAlarmHistoryId().getId().equals(T)) {
                id = alarmHistory.getId();
                 }
             }
        AlarmHistory oneData = alarmHistoryRepository.findOne(id);
        return oneData;
        }

    /**
     * 入库作废时间
     * @param id
     * @param date
     */
    public void saveDestroy(String id, Date date) {
        String alarmNameContent = "";
        Alarm oneAlarm = alarmRepository.findOne(id);
        String equipmentName = oneAlarm.getEquipmentName();
        String alarmContent = oneAlarm.getAlarmContent();
        alarmNameContent = equipmentName+"-"+alarmContent;
        //作废将报警
        AlarmHistory oneData = findAllAlarmDataById(id);
        AlarmHistory alarmHistory = new AlarmHistory();
        alarmHistory.setId(oneData.getId());
        alarmHistory.setAlarmHistoryId(oneData.getAlarmHistoryId());
        alarmHistory.setAlarmDestroyTime(date);//作废日期
        alarmHistory.setAlarmNameContent(alarmNameContent);
        alarmHistory.setAlarmStartTime(oneAlarm.getCreateTime());
        alarmHistory.setAlarmAffirmTime(oneData.getAlarmAffirmTime());
        if(oneData.getAlarmDisposeTime()!=null){
            alarmHistory.setAlarmDisposeTime(oneData.getAlarmDisposeTime());//处理日期
        }else {
            alarmHistory.setAlarmDisposeTime(date);
        }
        alarmHistory.setAlarmDealIdea(oneAlarm.getDealIdea());
        alarmHistory.setAlarmOpertateContent(oneData.getAlarmOpertateContent());
        alarmHistoryRepository.save(alarmHistory);
    }

    /**
     * 入库确认时间
     * @param id
     * @param date
     */
    public void saveDispose(String id, Date date) {
        AlarmHistory oneData = findAllAlarmDataById(id);
        AlarmHistory alarmHistory = new AlarmHistory();
        alarmHistory.setId(oneData.getId());
        alarmHistory.setAlarmHistoryId(oneData.getAlarmHistoryId());
        alarmHistory.setAlarmNameContent(oneData.getAlarmNameContent());
        alarmHistory.setAlarmStartTime(oneData.getAlarmStartTime());
        alarmHistory.setAlarmAffirmTime(oneData.getAlarmAffirmTime());
        alarmHistory.setAlarmDisposeTime(date);
        alarmHistory.setAlarmDestroyTime(oneData.getAlarmDestroyTime());
        alarmHistory.setAlarmDealIdea(oneData.getAlarmDealIdea());
        alarmHistory.setAlarmOpertateContent(oneData.getAlarmOpertateContent());
        alarmHistoryRepository.save(alarmHistory);
    }

    /**
     * 保存备注信息
     * @param id
     * @param operateContent
     */
    public void saveOperateContent(String id, String operateContent) {
        AlarmHistory oneData = findAllAlarmDataById(id);
        AlarmHistory alarmHistory = new AlarmHistory();
        alarmHistory.setId(oneData.getId());
        alarmHistory.setAlarmHistoryId(new Alarm(id));
        alarmHistory.setAlarmNameContent(oneData.getAlarmNameContent());
        alarmHistory.setAlarmStartTime(oneData.getAlarmStartTime());
        alarmHistory.setAlarmAffirmTime(oneData.getAlarmAffirmTime());
        alarmHistory.setAlarmDisposeTime(oneData.getAlarmDisposeTime());
        alarmHistory.setAlarmDestroyTime(oneData.getAlarmDestroyTime());
        alarmHistory.setAlarmDealIdea(oneData.getAlarmDealIdea());
        alarmHistory.setAlarmOpertateContent(operateContent);
        alarmHistoryRepository.save(alarmHistory);
    }

    //数据确认时间
    public void saveAffirm(String id, Date date) {
        AlarmHistory alarmHistory = new AlarmHistory();
        AlarmHistory oneData = findAllAlarmDataById(id);
        if(oneData!=null){
            alarmHistory.setId(oneData.getId());
            alarmHistory.setAlarmHistoryId(oneData.getAlarmHistoryId());
            alarmHistory.setAlarmNameContent(oneData.getAlarmNameContent());
            alarmHistory.setAlarmStartTime(oneData.getAlarmStartTime());
            alarmHistory.setAlarmAffirmTime(date);
            alarmHistory.setAlarmDisposeTime(oneData.getAlarmDisposeTime());
            alarmHistory.setAlarmDestroyTime(oneData.getAlarmDestroyTime());
            alarmHistory.setAlarmDealIdea(oneData.getAlarmDealIdea());
            alarmHistory.setAlarmOpertateContent(oneData.getAlarmOpertateContent());
            alarmHistoryRepository.save(alarmHistory);
        }else{
            alarmHistory.setAlarmHistoryId(new Alarm(id));
            alarmHistory.setAlarmAffirmTime(date);
            alarmHistoryRepository.save(alarmHistory);
        }
    }
    /**
     * 通过id的展示历史记录表
     * @param id
     */
    public AlarmHistory findByIdData(String id){
        AlarmHistory data = findAllAlarmDataById(id);
        return data;
    }
}
